package cn.edu.swu;

import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.annotation.WebFilter;

public class AuthFilter extends HttpFilter {

    private static final long serialVersionUID = -2840770222932338931L;

    public final static String LOGIN_STATUS = "LOGIN_STATUS";
    public final static String LOGIN_VALIDATE_CODE = "LOGIN_VALIDATE_CODE";
    public final static String REGISTER_STATUS = "REGISTER_STATUS";
    public final static String REGISTER_VALIDATE_CODE = "REGISTER_VALIDATE_CODE";

    public void doFilter(HttpServletRequest request,
                         HttpServletResponse response, FilterChain chain)
            throws java.io.IOException, ServletException {

        HttpSession session = request.getSession(true);
        Boolean status = (Boolean) session.getAttribute(LOGIN_STATUS);
        Boolean status2 = (Boolean) session.getAttribute(REGISTER_STATUS);

        if (status == null || status.equals(Boolean.FALSE)) {
            response.sendRedirect("/demo/login.jsp");
        } else {
            chain.doFilter(request, response);
        }

        if (status2==null || status2.equals(Boolean.FALSE)) {
            response.sendRedirect(("/demo/register.html"));
        } else {
            chain.doFilter(request,response);
        }
    }

}